package com.template.cms.web.dto;

import java.util.List;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;

import com.template.auth.model.User;

public class UserDto {

	private Integer id;

    private String username;

    private String password;

    private String email;

    private String phone;

    private String weixin;
    
    private Integer[] roleIds;
    
	private Integer[] groupIds;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
	@NotEmpty(message="用户名不能为空")
	public String getUsername() {
		return username;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}
	
	@NotEmpty(message="用户密码不能为空")
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	@Email(message="邮件格式不正确")
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getWeixin() {
		return weixin;
	}

	public void setWeixin(String weixin) {
		this.weixin = weixin;
	}

	public Integer[] getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(Integer[] roleIds) {
		this.roleIds = roleIds;
	}

	public Integer[] getGroupIds() {
		return groupIds;
	}

	public void setGroupIds(Integer[] groupIds) {
		this.groupIds = groupIds;
	}
	
	public User getUser(){
		User u = new User();
		u.setId(this.id);
		u.setUsername(username);
		u.setPassword(password);
		u.setEmail(email);
		u.setPhone(phone);
		u.setWeixin(weixin);
		return u;
	}
	
	public UserDto(User user){
		this.setEmail(user.getEmail());
		this.setId(user.getId());
		this.setPassword(user.getPassword());
		this.setPhone(user.getPhone());
		this.setUsername(user.getUsername());
		this.setWeixin(user.getWeixin());
	}
	
	public UserDto(User user, Integer[] roleIds, Integer[] groupIds){
		this.setEmail(user.getEmail());
		this.setId(user.getId());
		this.setPassword(user.getPassword());
		this.setPhone(user.getPhone());
		this.setUsername(user.getUsername());
		this.setWeixin(user.getWeixin());
		this.setGroupIds(groupIds);
		this.setRoleIds(roleIds);
	}
	public UserDto(User user, List<Integer> roleIds, List<Integer> groupIds){
		this.setEmail(user.getEmail());
		this.setId(user.getId());
		this.setPassword(user.getPassword());
		this.setPhone(user.getPhone());
		this.setWeixin(user.getWeixin());
		this.setUsername(user.getUsername());
		this.setGroupIds(listToArray(groupIds));
		this.setRoleIds(listToArray(roleIds));
	}
	
	private Integer[] listToArray(List<Integer> list){
		Integer[] a = new Integer[list.size()];
		for(Integer in : list){
			a[list.indexOf(in)] = in;
		}
		return a;
	}
	public UserDto(){
	}
	
}
